OpenRoads Designer CONNECT Edition SDK Help

Annotate plan element

The below code snippet shows annotating the plan element. Here in the code alignment element is annotated with the station values along an interval.


//Required References
using System;
using Bentley.DgnPlatformNET;
using Bentley.MstnPlatformNET;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.GeometryNET;
using Bentley.DgnPlatformNET.Elements;
using System.Diagnostics;
using Bentley.CifNET.LinearGeometry;

public void AnnotatePlan()
        {

            try
            {
                //Get active dgn model
                DgnModel model = Session.Instance.GetActiveDgnModel();

                //Get active geometric model to get alignments
                Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit con = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
                GeometricModel geomModel = con.GetActiveGeometricModel();

                //Get all alignments from current DGN 
                foreach (Alignment alignment in geomModel.Alignments)
                {
                    if (!alignment.IsFinalElement)
                        continue;

                    //Get the alignment points at defined length and offset for placing the annotation
                    if (alignment != null)
                    {
                        LinearElement LinearEl = alignment.LinearGeometry;
                        double EndPosition = LinearEl.Length;
                        double Interval = 100;
                        double Length = 20;
                        for (double dStation = 0; EndPosition > dStation - Interval; dStation += Interval)
                        {
                            if (dStation > EndPosition)
                                dStation = EndPosition;
                            LinearPoint offsetPt0 = null, offsetPtLength = null;
                            offsetPt0 = LinearEl.GetPointAtDistanceOffset(dStation, 0);
                            offsetPtLength = LinearEl.GetPointAtDistanceOffset(dStation, -Length);

                            //User need unit conversion here based on unit settings 
                            DPoint3d startPoint = offsetPt0.Coordinates;
                            
                            //User need unit conversion here based on unit settings 
                            DPoint3d endPoint = offsetPtLength.Coordinates;
                          
                            DSegment3d segment = new DSegment3d(startPoint, endPoint);
                            LineElement lineElement = new Bentley.DgnPlatformNET.Elements.LineElement(model, null, segment);
                            lineElement.AddToModel();

                            DVector3d rotVector = segment.UnitTangent;
                            DMatrix3d rotMatrix = DMatrix3d.Rotation(2, rotVector.AngleXY);

                            //Create new DgnTextStyle element for annotation definition
                            DgnFile activeDgnFile = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnFile();
                            DgnTextStyle textStyle = DgnTextStyle.GetByName("Line Length Label", activeDgnFile);

                            if (null == textStyle)
                            {
                                textStyle = new DgnTextStyle("Line Length Label", activeDgnFile);
                                textStyle.SetProperty(TextStyleProperty.Width, 400D);
                                textStyle.SetProperty(TextStyleProperty.Height, 400D);
                                textStyle.Add(activeDgnFile);
                            }

                            //User might need unit conversion for dStation based on unit settings 
                            string textString = string.Format("sta = {0:F1}", dStation);

                            //New TextBlock for defining annotation
                            TextBlock textBlock = new TextBlock(textStyle, model);
                            textBlock.AppendText(textString);
                            textBlock.SetUserOrigin(startPoint);
                            textBlock.SetOrientation(rotMatrix);

                            //Add annotation
                            TextElement textElement = (TextElement)TextElement.CreateElement(null, textBlock);
                            textElement.AddToModel();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            return;
        }

Output

The above code annotates alignments as shown in below image